package com.itheima.app.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.huanxin.HuanXinTemplate;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.RecommendUser;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.UserLikeCountVo;
import com.itheima.domain.vo.UserLikeVo;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.FriendService;
import com.itheima.service.mongo.RecommendUserService;
import com.itheima.service.mongo.UserLikeService;
import com.itheima.util.ConstantUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class UserLikeManager {

    @Reference
    private UserLikeService userLikeService;

    @Reference
    private FriendService friendService;

    @Autowired
    private HuanXinTemplate huanXinTemplate;
    /**
     * 用户喜欢
     * @param likeUserId
     */
    public void likeUser(Long likeUserId) {
        //1.获得当前登录用户id
        Long loginUserId = UserHolder.getId();
        //2.添加mongo的数据 喜欢
        boolean flag = userLikeService.likeUser(loginUserId  ,likeUserId );
        //3.添加环信好友的动作
        if(flag){
            //互相喜欢了 添加环信的好友关系(环信中双方都需要在环信中进行注册)
            huanXinTemplate.addContacts(loginUserId , likeUserId);
        }

    }

    /**
     * 互相喜欢，喜欢，粉丝 - 统计
     * @return
     */
    public UserLikeCountVo findCounts() {
        Long loginUserId = UserHolder.getId();
        Long count1= userLikeService.findEachLoveCount(loginUserId);//查询互相喜欢
        Long count2= userLikeService.findFanCount(loginUserId);//查询粉丝
        Long count3= userLikeService.findLoveCount(loginUserId);//查询我的喜欢

        UserLikeCountVo vo = new UserLikeCountVo();
        vo.setEachLoveCount(count1);
        vo.setFanCount(count2);
        vo.setLoveCount(count3);
        return vo;
    }

    @Reference
    private RecommendUserService recommendUserService;
    @Reference
    private UserInfoService userInfoService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    //查询 互相喜欢，喜欢，粉丝 访客 数据
    //1 互相关注
    //2 我关注
    //3 粉丝
    //4 谁看过我
    public PageBeanVo findUserInfoByType(Integer page, Integer pageSize, String type) {
        //1.获得当前的用户id
        Long loginUserId = UserHolder.getId();
        //2.根据 用户的id 和 type取值 获得数据
        PageBeanVo pageBeanVo = userLikeService.findUserInfoByType(page , pageSize,  type , loginUserId  );
        //3.数据封装
        List<UserLikeVo> items = (List<UserLikeVo>) pageBeanVo.getItems();
        if(CollectionUtil.isNotEmpty(items)){ //如果数据存在 进一步封装用户数据
            for (UserLikeVo userLikeVo : items) {
                //3.1 查询用户数据
                UserInfo userInfo = userInfoService.findById(userLikeVo.getId());
                //3.2 将用户数据封装到vo中
                BeanUtil.copyProperties( userInfo , userLikeVo  );
                //3.2 处理其他的数据 例如缘分值
                RecommendUser recommendUser = recommendUserService.findFateValue(loginUserId, userLikeVo.getId());
                userLikeVo.setMatchRate(recommendUser.getScore().intValue());//设置缘分值
                //是否喜欢它  查询数据库中是否喜欢过这个用户  如果是 返回true 如果不是返回false
                //实战需求
                //userLikeVo.setAlreadyLove();
            }
        }
        System.out.println("===>>>" + pageBeanVo);

        if(StrUtil.equals(type, "4")){
            //更新一下最后的查询访客时间
            stringRedisTemplate.opsForValue().set(ConstantUtil.LAST_ACCESS_TIME + loginUserId , System.currentTimeMillis()+"" );
        }

        return pageBeanVo;
    }

    //粉丝 - 喜欢
    public void saveFanLike(Long userId) {
        Long loginUserId = UserHolder.getId();
        userLikeService.saveUserLike(userId,loginUserId);
        //判断双方是否互相喜欢
        Boolean flag =  userLikeService.isLike(userId, loginUserId);
        if (flag) {
            friendService.addContacts(userId, loginUserId);
            huanXinTemplate.addContacts(loginUserId, userId);
        }
    }

    //喜欢 - 取消
    public void deleteLike(Long userId) {
        Long loginUserId = UserHolder.getId();
        userLikeService.deleteUserLike(userId,loginUserId);
        //删除friend表中的数据
        friendService.deleteContacts(userId, loginUserId);
        //接触环信中的好友关系
        huanXinTemplate.deleteContacts(loginUserId, userId);
    }

}
